
/**  
* @Title: Operater.java  
* @Package org.sapling.data.beans  
* @Description: TODO
* @author DcfZero  
* @date 2021年5月14日  
* @version V1.0  
*/ 
package org.sapling.data.beans;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import lombok.Setter;

/**  
* TODO
* @ClassName: DataOperater  
* @author DcfZero 
* @date 2021年5月14日  
*    
*/
@Setter
public class DataOperater {
	
	private String tableId;
	private String whereString;
	private String orderString;
	private String selectColumns;
	
	private int rows = 10;
	private boolean isTranslate = false;
	private int pagination = 0;
	
	
	public Operate operate = Operate.QUERY;
	
	public DataOperater(String tableId) {
		Assert.hasLength(tableId,"无法创建一个空的数据操作对象");
		this.tableId = tableId;
	}
	
	public DataOperater(String tableId,Operate operate) {
		Assert.hasLength(tableId,"无法创建一个空的数据操作对象");
		Assert.notNull(operate, "无法创建一个空类型的数据操作");
		this.tableId = tableId;
		this.operate = operate;
	}
	
	private String getSql(String tableName) {
		switch (this.operate) {
		case QUERY:
			return getQuerySql(tableName);
		case UPDATE:
			return getUpdateSql(tableName);
		case DELETE:
			return getDelSql(tableName);
		}
		return "";
	}
	
	
	
	
	/**  
	* @Title: getDelSql  
	* @return void  返回类型  
	* @throws  
	*/ 
	private String getDelSql(String tableName) {
		StringBuilder sql = (new StringBuilder("DELETE FROM ")).append(tableName);
	    if (StringUtils.isNotBlank(this.whereString))
	      sql.append(" WHERE ").append(this.whereString); 
	    return sql.toString();
	}

	/**  
	* @Title: getUpdateSql  
	* @return void  返回类型  
	* @throws  
	*/ 
	private String getUpdateSql(String tableName) {
		StringBuilder sql = new StringBuilder("UPDATE ");
		sql.append(tableName).append(" SET ").append(this.selectColumns);
	    if (StringUtils.isNotBlank(this.whereString))
	      sql.append(" WHERE ").append(this.whereString); 
	    return sql.toString();
	}

	/**  
	* @Title: getQuerySql  
	* @return void  返回类型  
	* @throws  
	*/ 
	public String getQuerySql(String tableName) {
		StringBuilder sql = new StringBuilder("SELECT ");
		sql.append(StringUtils.isBlank(this.selectColumns) ? "*" : this.selectColumns).append(" FROM ").append(tableName);
		if(!StringUtils.isBlank(this.whereString)) {
			sql.append(" WHERE ").append(this.whereString);
		}
		if(!StringUtils.isBlank(this.orderString)) {
			sql.append(" ORDER BY ").append(this.orderString);
		}
		return sql.toString();
	}
	
	public String getTableId() {
		return this.getTableId();
	}
	
	public boolean isTranslate() {
		return this.isTranslate;
	}
	
	public int getRows() {
		return rows;
	}

	public int getPagination() {
		return pagination;
	}
	
	public String getWhereString() {
		return whereString;
	}

	public String getOrderString() {
		return orderString;
	}

	public String getSelectColumns() {
		return selectColumns;
	}





	public enum Operate{
		QUERY,
		UPDATE,
		DELETE;
	}
}
